package org.siscoh.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.siscoh.model.bean.AbstractBean;
import org.siscoh.model.bean.UsuarioBean;

/**
*/
public class UsuarioDao extends AbstractDao {

    @Override
    public void update(AbstractBean bean) {
        
        UsuarioBean user = (UsuarioBean) bean;
        
        Conexao.getInstance().update("usuario",
                new String[]{"login"        ,"nome"        ,"senha"        ,"administrador"},
                new String[]{user.getLogin(),user.getNome(),user.getSenha(),user.getAdministrador()+""},
                "id = '"+user.getId()+"'");
    }

    @Override
    public void insert(AbstractBean bean) {
        UsuarioBean user = (UsuarioBean) bean;
        
        Conexao.getInstance().insert("usuario",
                new String[]{"login"        ,   "nome"     ,   "senha"     ,    "administrador"     },
                new String[]{user.getLogin(),user.getNome(),user.getSenha(),user.getAdministrador()+""});
        
    }

    @Override
    public void delete(AbstractBean bean) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<AbstractBean> fetchAllArrayList() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public AbstractBean[] fetchAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public UsuarioBean verificarLogin(String login, String senha)
    {
        UsuarioBean ub = null;
        
        ResultSet rs = Conexao.getInstance().select(null, "usuario","login = '"+login+"'");
        ub = (UsuarioBean) parseRsToObject(rs);
        
        /*
        System.err.println("------------------------------------------");
        System.err.println("----------------USUARIOS------------------");
        System.err.println("------------------------------------------");
        */
        
        if(ub.getLogin().compareTo(login) == 0 && ub.getSenha().compareTo(senha) == 0)
            return ub;
        
        return null;
    }
    
    @Override
    public AbstractBean selectById(long id) {
        UsuarioBean user = null;
        
        ResultSet rs = Conexao.getInstance().select(null, "usuario", "id = '"+id+"'");
        
        user = (UsuarioBean) parseRsToObject(rs);
        
        return user;
    }

    @Override
    protected AbstractBean parseRsToObject(ResultSet rs) {
        UsuarioBean ub = null;
        
        try {
            if(rs.next())
            {
                ub = new UsuarioBean();
                
                ub.setId(rs.getLong("id"));
                ub.setNome(rs.getString("nome"));
                ub.setLogin(rs.getString("login"));
                ub.setSenha(rs.getString("senha"));
                ub.setAdministrador(rs.getBoolean("administrador"));
                
            }
        } catch (SQLException ex) {
            ex.printStackTrace(System.err);
        }
        
        return ub;
    }

    @Override
    protected ArrayList<AbstractBean> parseRsToObjects(ResultSet rs) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

